package com.sita.linboard.driverInfo;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.sita.linboard.R;
import com.sita.linboard.base.BaseActivity;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Jason on 2016/9/2.
 */
public class MsgListAdapter extends BaseAdapter {
    private Context context;
    private List<MsgCenterBackBean.DataBean> backBean;
    private int PAYMESSAGE = 3;//支付消息
    private int SYSTEMMESSAGE = 2; //系统消息
    private int REMINDMESSAGE = 1; //提示消息
    private int ACTIVITYMESSAGE =4; //活动消息

    public MsgListAdapter(Context context) {
        this.context = context;
        backBean = new ArrayList<>();
    }
    public void setData(List<MsgCenterBackBean.DataBean> data,boolean clear){
        if (clear){
            backBean.clear();
        }
        else {
            backBean.addAll(data);
        }
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        if (backBean!=null){
            return backBean.size();
        }
        else {
            return 0;
        }

    }

    @Override
    public Object getItem(int position) {
        return backBean.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public int getItemViewType(int position) {
        if (backBean.get(position).getType()==3){
            return PAYMESSAGE;
        }
        else if (backBean.get(position).getType()==2){
            return SYSTEMMESSAGE;
        }
        else if (backBean.get(position).getType()==1){
            return REMINDMESSAGE;
        }
        else if(backBean.get(position).getType()==4){
            return ACTIVITYMESSAGE;
        }
        else {
            return 0;
        }

    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        MyViewHolder holder =null;
        if (convertView ==null){
            holder = new MyViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.item_msg_center,null);
            holder.icon = (ImageView) convertView.findViewById(R.id.msg_icon);
            holder.title = (TextView) convertView.findViewById(R.id.title_msg);
            holder.message = (TextView) convertView.findViewById(R.id.info_msg);
            holder.time = (TextView) convertView.findViewById(R.id.time_msg);
            convertView.setTag(holder);
        }
        else {
            holder = (MyViewHolder) convertView.getTag();
        }
        if (getItemViewType(position)==PAYMESSAGE){
            holder.title.setText("支付消息");
            holder.icon.setImageResource(R.mipmap.pay_icon);
        }
        else if (getItemViewType(position)==SYSTEMMESSAGE){
            holder.title.setText("系统消息");
            holder.icon.setImageResource(R.mipmap.system_icon);
        }
        else if (getItemViewType(position)==REMINDMESSAGE){
            holder.title.setText("温馨提示");
            holder.icon.setImageResource(R.mipmap.warm_icon);
        }
        else if (getItemViewType(position)==ACTIVITYMESSAGE){
            holder.title.setText("活动消息");
            holder.icon.setImageResource(R.mipmap.activity_icon);
        }
        holder.message.setText(backBean.get(position).getInfo());
        holder.time.setText(backBean.get(position).getCreate_time());


        return convertView;
    }
    class MyViewHolder {
        TextView title,message,time;
        ImageView icon;

    }
}
